package com.colin.app.popupwindow;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.library.view.help.MetricsHelp;

import java.util.Arrays;
import java.util.List;

public class ListViewPopupWindow extends PopupWindow {
    public interface OnListViewInPopupWindowListener {
        void onItemCheck(int position, String string);
    }

    private Context context;
    private View view;
    private TextView text_pop_title;
    private ListView list_pop_content;
    private OnListViewInPopupWindowListener onListViewInPopupwindowListener = null;


    public ListViewPopupWindow(Context context, String[] strings, String title) {
        super(context);
        this.context = context;
        initView(title);
        initData(Arrays.asList(strings));
    }

    private void initView(String title) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_pop_list, null);
        this.list_pop_content = (ListView) view.findViewById(R.id.list_pop_content);
        // 设置允许在外点击消失
        this.setOutsideTouchable(false);
        // 设置SelectPicPopupWindow的View
        this.setContentView(view);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(MetricsHelp.dp2px(context, 100));
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
//        this.setHeight((MetricsUtils.getWindowsHeight((Activity) context) / 5) * 3);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(android.R.style.Animation_InputMethod);
        // 设置背景色
        Resources resources = view.getResources();
        Drawable drawable = resources.getDrawable(android.R.color.white);
        this.setBackgroundDrawable(drawable);
    }

    private void initData(final List<String> stirngList) {
        list_pop_content.setAdapter(new ArrayAdapter<String>(context, R.layout.item_text_show, R.id.text_item_content, stirngList));
        list_pop_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onListViewInPopupwindowListener != null) {
                    onListViewInPopupwindowListener.onItemCheck(position, stirngList.get(position));
                    dismiss();
                }
            }
        });
    }

    public void setOnListViewInPopupwindowListener(OnListViewInPopupWindowListener listener) {
        onListViewInPopupwindowListener = listener;
    }

}
